package controller;

import com.root.health.bean.UserInfo;
import com.root.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    /**
     *
     * 以用户名获取用户信息
     */

    @GetMapping("userInfoManager")
    public String getByName(UserInfo userInfo,HttpSession session, Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }
        UserInfo tempo_user = (UserInfo) session.getAttribute("loginUser");
        userInfo = userService.getUserbyName(tempo_user.getUserName());//获取当前用户信息
        if(userInfo.getRole().equals("children")){
            userInfo.setRole("子女");
        }else if(userInfo.getRole().equals("parents")){
            userInfo.setRole("父母");
        }else{
            model.addAttribute("errormsg","用户角色错误");
            return "error";
        }
        if(userInfo.getGender().equals("male")){
            userInfo.setGender("男");
        }else if(userInfo.getGender().equals("female")){
            userInfo.setGender("女");
        }else{
            model.addAttribute("errormsg","用户性别是错误的");
            return "error";
        }
        model.addAttribute("userInfo",userInfo);
        return "userInfoManager";
    }

    /**
     *
     * 修改用户信息
     */
    @GetMapping("userinfoUpd")
    public  String userUpd(){
        return "userinfoUpd";
    }
    @PostMapping("userinfoUpd")
    public String userUpd(UserInfo userInfo, HttpSession session, Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }
        UserInfo tempo_user = (UserInfo) session.getAttribute("loginUser");
        UserInfo a_user = userService.getUserbyName(tempo_user.getUserName());
        userInfo.setUserId(a_user.getUserId());
        //由于Session传递来的用户是前端页面中输入的用户实体类，没有被数据库增加userid，所以需要再从数据库查询一次当前用户获取userid
            userService.userUpd(
                    userInfo.getUserName(),userInfo.getPassword(),userInfo.getPhone(),
                    userInfo.getRealname(),userInfo.getRole(),userInfo.getGender(),userInfo.getUserId());
            model.addAttribute("successHref","userInfoManager");
                        model.addAttribute("successHref","userInfoManager");
            return "success";

    }
    @GetMapping("exit")
    public String exitUser(HttpSession session,Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }
        session.removeAttribute("loginUser");
        return "login";
    }
//    @ResponseBody
//    @GetMapping("userrole")
//    public Boolean UserRole(UserInfo userInfo){
//        if(userInfo.getRole().equals("parent")){
//            return true;
//        }else if(userInfo.getRole().equals("children")){
//            return false;
//        }
//        return userService.UserRole(userInfo);
//    }//判断用户角色
}
